const api = require("../../utils/api")
const cache = require("../../utils/cache")
// pages/user/bindCard.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    TabCur: 0,
    picker: ['身份证', '护照', '香港身份证', '澳门身份证', '台湾身份证'],
    pickerSex: ['男', '女'],
    name: '', //姓名
    cardType: '身份证', //卡类型
    cardNo: '', //卡号
    phone: '', //手机号
    checkNum: '', //验证码
    ioNo: '', //就诊卡号

    indexSex: '', //儿童性别
    name_child: '', //姓名 儿童
    cardType_child: '身份证', //卡类型 儿童
    cardNo_child: '', //监护人证件号码
    phone_child: '', //监护人手机号码
    checkNum_child: '', //监护人手机号码验证码
    ioNo_child: '', //儿童就诊卡号
  },
  //获取验证码
  handleSendVerifyCode() {
    if (this.data.phone == '' || this.data.phone == null || this.data.phone == undefined) {
      return;
    }
    let phoneNumber = this.data.phone
    let that = this
    api.sendVerifyCode("+86" + phoneNumber).then(res => {
      if (!res) {
        return;
      }
      let count = 0
      let id = setInterval(() => {
        count++;
        if (count >= 60) {
          that.setData({
            verify: false,
            surplus: 0
          })
          clearInterval(id);
        } else {
          that.setData({
            verify: true,
            surplus: 60 - count
          })
        }
      }, 1000);
    })
  },
  //获取监护人验证码
  handleSendChildVerifyCode() {
    if (this.data.phone_child == '' || this.data.phone_child == null || this.data.phone_child == undefined) {
      return;
    }
    let phoneNumber = this.data.phone_child
    let that = this
    api.sendVerifyCode("+86" + phoneNumber).then(res => {
      if (!res) {
        return;
      }
      let count = 0
      let id = setInterval(() => {
        count++;
        if (count >= 60) {
          that.setData({
            verify_child: false,
            surplus_child: 0
          })
          clearInterval(id);
        } else {
          that.setData({
            verify_child: true,
            surplus_child: 60 - count
          })
        }
      }, 1000);
    })
 
  },
  handleCreatePatient() {
    wx.navigateTo({
      url: '/pages/user/createCard?delta=2',
    })
  },
  handleGetIoNo() {
    if (this.data.cardNo == '' || this.data.cardNo == null || this.data.cardNo == undefined) {
      wx.showToast({
        title: '请先输入身份证号码',
        icon: 'none'
      })
      return;
    }
    let that = this
    api.getIoNo({
      child: 'N',
      cardNo: that.data.cardNo
    }).then(res => {
      that.setData({
        ioNo: res
      })
    })
  },
  handleGetChildIoNo() {
    if (this.data.cardNo_child == '' || this.data.cardNo_child == null || this.data.cardNo_child == undefined) {
      wx.showToast({
        title: '请先输入监护人身份证号码',
        icon: 'none'
      })
      return;
    }
    let that = this
    api.getIoNo({
      child: 'Y',
      cardNo: that.data.cardNo_child
    }).then(res => {
      that.setData({
        ioNo_child: res
      })
    })
  },
  handleBindPatient(e) {
    this.setData({
      loadModal: true
    })
    let that = this
    setTimeout(() => {
      that.setData({
        loadModal: false
      })
    }, 10000);
    let patient = {
      name: this.data.TabCur == 0 ? this.data.name : this.data.name_child,
      cardno: this.data.TabCur == 0 ? this.data.cardNo : '',
      cardtype: '',
      phone: this.data.TabCur == 0 ? this.data.phone : this.data.phone_child,
      sex: '',
      child: '',
      guardianCardtype: '',
      guardianCardno: '',
      crtFlag: '',
      ioNo: this.data.TabCur == 0 ? this.data.ioNo : this.data.ioNo_child,
      checkNum: this.data.TabCur == 0 ? this.data.checkNum : this.data.checkNum_child,
    }
    if (this.data.TabCur == 0) { //大人小孩
      patient.child = "N"
      if (this.data.cardType == "身份证") {
        patient.cardtype = "01"
      } else if (this.data.cardType == "护照") {
        patient.cardtype = "02"
      } else if (this.data.cardType == "香港身份证") {
        patient.cardtype = "03"
      } else if (this.data.cardType == "澳门身份证") {
        patient.cardtype = "04"
      } else if (this.data.cardType == "台湾身份证") {
        patient.cardtype = "05"
      }
      if (patient.cardtype == "01" && patient.cardno != '') {
        let sexBit = patient.cardno.substr(16, 1);
        if (sexBit % 2 == 0) { //男单女双
          patient.sex = "M"
        } else {
          patient.sex = "F"
        }
      }
    } else {
      patient.child = "Y"
      if (this.data.cardType_child == "身份证") {
        patient.guardianCardtype = "01"
      } else if (this.data.cardType_child == "护照") {
        patient.guardianCardtype = "02"
      } else if (this.data.cardType_child == "香港身份证") {
        patient.guardianCardtype = "03"
      } else if (this.data.cardType_child == "澳门身份证") {
        patient.guardianCardtype = "04"
      } else if (this.data.cardType_child == "台湾身份证") {
        patient.guardianCardtype = "05"
      }
      patient.guardianCardno = this.data.cardNo_child
      if (this.data.pickerSex[this.data.indexSex] == "男") {
        patient.sex = "F"
      } else {
        patient.sex = "M"
      }
    }

    api.bindPatient(patient).then(res => {
      cache.setValue("patient", res);
      wx.navigateBack({
        delta: 1
      });
    }).finally(() => {
      that.setData({
        loadModal: false
      })
    })
  },
  tabSelect(e) {
    this.setData({
      TabCur: e.currentTarget.dataset.id,
    })
  },
  PickerChange(e) {
    console.log(e);
    this.setData({
      index: e.detail.value
    })
  },
  PickerChangeSex(e) {
    console.log(e);
    this.setData({
      indexSex: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})